package com.sykj.bsc.rwl.service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sykj.bsc.rwl.mapper.SysRoleMapper;
import com.sykj.bsc.rwl.po.SysDept;
import com.sykj.bsc.rwl.po.SysRole;
import com.sykj.bsc.rwl.po.ZtreeBean;
import com.sykj.bsc.rwl.service.SysRoleService;
import com.sykj.bsc.rwl.util.Code;
import com.sykj.bsc.rwl.util.JedisClientPool;
import com.sykj.bsc.rwl.util.JsonUtils;
@Service("sysRoleServiceImpl")
public class SysRoleServiceImpl implements SysRoleService {
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	private ZtreeBean ztreeBean;
	/*@Autowired
	private JedisClientPool pool;*/
	
	//查询所有角色信息
	public List<SysRole> selectSysRole(HashMap<String, String> map) throws Exception {
		//String json=pool.get("SysRoleMapr1");
		List<SysRole> list=sysRoleMapper.selectSysRole(map);
		/*if(json==null) {
			list=sysRoleMapper.selectSysRole(map);
			json=JsonUtils.objectToJson(list);
			pool.set("SysRoleMapr1", json);
		}else {
			if(map!=null) {
				list=sysRoleMapper.selectSysRole(map);
				json=JsonUtils.objectToJson(list);
				pool.set("SysRoleMapr1", json);
			}
			list=JsonUtils.jsonToList(json, SysRole.class);
		}*/
		return list;
	}

	//删除角色
	public void deleteSysRole(Integer role_id) throws Exception {
		sysRoleMapper.deleteSysRole(role_id);
	}

	@Override
	public SysRole findBySysRole(HashMap<String, String> map) throws Exception {
		//String json=pool.get("findBySysRoleMapr2");
		SysRole role=sysRoleMapper.findBySysRole(map);
		/*if(json==null) {
			role=sysRoleMapper.findBySysRole(map);
			json=JsonUtils.objectToJson(role);
			pool.set("findBySysRoleMapr2", json);
		}else {
			if(map!=null) {
				role=sysRoleMapper.findBySysRole(map);
				json=JsonUtils.objectToJson(role);
				pool.set("findBySysRoleMapr2", json);
			}
			role=JsonUtils.jsonToPojo(json, SysRole.class);
		}*/
		return role;
	}

	@Override
	public void insertSysRole(SysRole sysRole) throws Exception {
		String role_id2=sysRole.getRole_id2();
		HashMap<String, String>map=new HashMap<>();
		HashMap<String, String>map2=new HashMap<>();
		HashMap<String, String>map3=new HashMap<>();
		map.put("role_id2", role_id2);
		SysRole sys=sysRoleMapper.findByIdSysRole(map);
		/*String json=pool.get("sysRoleMapSysr3");
		if(json==null) {
			sys=sysRoleMapper.findByIdSysRole(map);
			json=JsonUtils.objectToJson(sys);
			pool.set("sysRoleMapSysr3", json);
		}else {
			sys=JsonUtils.jsonToPojo(json, SysRole.class);
		}*/
		String cascade_id=sys.getCascade_id();
		String parent_id=sysRole.getParent_id()+"";
		map2.put("parent_id", parent_id);
		int count=sysRoleMapper.getCount(map2);
		String cascade_id2="";
		if(count==0) {
			cascade_id2=Code.getCode(cascade_id, cascade_id);
		}else {
			map3.put("cascade_id", cascade_id);
			map3.put("parent_id", parent_id);
			//String json2=pool.get("findBySysRoleMap3sr4");
			SysRole s=sysRoleMapper.findBySysRole(map3);
			/*if(json2==null) {
				s=sysRoleMapper.findBySysRole(map3);
				json2=JsonUtils.objectToJson(json2);
				pool.set("findBySysRoleMap3sr4", json2);
			}else {
				s=JsonUtils.jsonToPojo(json2,SysRole.class);
			}*/
			
			String chirden=s.getCascade_id();
			cascade_id2=Code.getCode(cascade_id, chirden);
		}
		sysRole.setCascade_id(cascade_id2);
		sysRole.setParent_id(Integer.valueOf(role_id2));
		sysRoleMapper.insertSysRole(sysRole);
	}

	@Override
	public String selectAllSysRole(HashMap<String, String> map) throws Exception {
		//String json=pool.get("selectSysRoleMaplistr5");
		List<SysRole> list= sysRoleMapper.selectSysRole(map);
		/*if(json==null) {
			list= sysRoleMapper.selectSysRole(map);
			json=JsonUtils.objectToJson(list);
			pool.set("selectSysRoleMaplistr5", json);
		}else {
			list=JsonUtils.jsonToList(json, SysRole.class);
		}*/
		List<ZtreeBean> list2=new ArrayList<ZtreeBean>();
		for(SysRole sysRole:list) {
			ztreeBean=new ZtreeBean();
			ztreeBean.setParentID(sysRole.getParent_id());
			ztreeBean.setResourceID(sysRole.getRole_id());
			ztreeBean.setResourceName(sysRole.getRole_name());
			ztreeBean.setAccessPath("/bsc/role/selectSysRole?cascade_id="+sysRole.getCascade_id());
			list2.add(ztreeBean);
		}
		String data=JsonUtils.objectToJson(list2);
		return data;
	}

	@Override
	public void updateSysRole(SysRole sysRole) throws Exception {
		sysRoleMapper.updateSysRole(sysRole);
	}

	@Override
	public SysRole findByIdSysRole(HashMap<String, String> map) throws Exception {
		SysRole role=sysRoleMapper.findByIdSysRole(map);
		/*String json=pool.get("findByIdSysRoleMaproler6");
		if(json==null) {
			role=sysRoleMapper.findByIdSysRole(map);
			json=JsonUtils.objectToJson(role);
			pool.set("findByIdSysRoleMaproler6", json);
		}else {
			role=JsonUtils.jsonToPojo(json, SysRole.class);
		}*/
		return role;
	}
}
